using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Facebook.Service.Core;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using FriendsNearby.Services.Facebook;

namespace FriendsNearby.Services
{
    public class FriendsNearbySystem : IServiceProvider
    {
        private ILocationService locationService;
        private IPeopleNearbyService peopleNearbyService;
        private ISocialNetworkService socialNetworkService;
        private IUsersService usersService;
        private ISocialNetworkServiceFactory facebookServiceFactory;

        public FriendsNearbySystem()
        {
            DataAccess dataAccess = PolicyInjection.Create<DataAccess>();

			facebookServiceFactory = new FacebookServiceFactory();
			usersService = new UsersService(locationService, dataAccess);
			socialNetworkService = new FacebookSocialNetworkService(facebookServiceFactory, dataAccess, usersService);
            locationService = new LocationService(dataAccess);
            peopleNearbyService = new PeopleNearbyService(locationService, usersService, socialNetworkService);


            //TODO: schedule for remove, find a way around this dep.
            ((LocationService)locationService).SetPeopleNearbyService(peopleNearbyService);
            
        }

        #region IServiceProvider Members

        public virtual object GetService(Type serviceType)
        {
            if (serviceType.IsAssignableFrom(typeof(ILocationService)))
                return locationService;
            if (serviceType.IsAssignableFrom(typeof(IPeopleNearbyService)))
                return peopleNearbyService;
            if (serviceType.IsAssignableFrom(typeof(ISocialNetworkService)))
                return socialNetworkService;            
            if (serviceType.IsAssignableFrom(typeof(IUsersService)))
                return usersService;
            if (serviceType.IsAssignableFrom(typeof(ISocialNetworkService)))
                return facebookServiceFactory;
            return null;
        }

        #endregion
    }
}
